package com.cslg.questionnaires.service;

import com.cslg.questionnaires.entity.Question;
import com.cslg.questionnaires.entity.QuestionForm;
import com.cslg.questionnaires.entity.Student;
import com.cslg.questionnaires.mapper.QuestionFormMapper;
import com.cslg.questionnaires.mapper.QuestionMapper;
import com.cslg.questionnaires.mapper.StudentMapper;
import com.cslg.questionnaires.utils.QuestionFormUtils;
import com.cslg.questionnaires.utils.QuestionUtils;
import com.cslg.questionnaires.utils.ServiceMessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class QuestionFormService {
    @Resource
    QuestionMapper questionMapper;
    @Resource
    StudentMapper studentMapper;
    @Resource
    QuestionFormMapper questionFormMapper;

    /**
     * 得到问卷信息
     * @param questionId
     * @return
     */
    public HashMap<String, Object> getQuestionForm(int questionId){
        Question question = questionMapper.getQuestionById(questionId);
        if (question == null){
            return ServiceMessageBuilder
                    .buildMessage(404,"表单不存在！",null);
        }

        int isexamine = question.getIsexamine();
        if (isexamine != 1){
            return ServiceMessageBuilder
                    .buildMessage(405,"表单不存在！",null);
        } else {
            Date nowdate = new Date();
            Date startdate = question.getStarttime();
            Date finishdate = question.getFinishtime();
            if (startdate.before(nowdate) && finishdate.after(nowdate)){
                HashMap<String, Object> questionForm = new HashMap<>();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String starttime = sdf.format(startdate);
                String finishtime = sdf.format(finishdate);
                String content_string = question.getContent();
                ArrayList content = QuestionUtils.parseQuestionContent(content_string);
                questionForm.put("id",question.getId());
                questionForm.put("title",question.getTitle());
                questionForm.put("content",content);
                questionForm.put("starttime",starttime);
                questionForm.put("finishtime",finishtime);
                return ServiceMessageBuilder
                        .buildMessage(200,"success",questionForm);
            } else if (startdate.after(nowdate) || startdate.equals(nowdate)){
                return ServiceMessageBuilder
                        .buildMessage(201,"表单还为能开始填写！",null);
            } else{
                // if (finishdate.before(nowdate) || finishdate.equals(nowdate))
                return ServiceMessageBuilder
                        .buildMessage(202,"表单填写已经截止！",null);
            }
        }
    }

    /**
     * 提交问卷填写结果
     * @param questionMap
     * @return
     */
    public HashMap<String,Object> submitQuestionForm(HashMap<String,Object> questionMap){
        int questionid = (int)questionMap.get("id");
        Question question = questionMapper.getQuestionById(questionid);
        // 验证问卷状态
        if (question == null){
            return ServiceMessageBuilder
                    .buildMessage(404,"表单不存在!",null);
        }else {
            Date nowdate = new Date();
            Date starttime = question.getStarttime();
            Date finishtime = question.getFinishtime();
            if (starttime.after(nowdate) || starttime.equals(nowdate)) {
                return ServiceMessageBuilder
                        .buildMessage(201, "表单还为能开始填写！", null);
            } else if (finishtime.before(nowdate) || finishtime.equals(nowdate)) {
                return ServiceMessageBuilder
                        .buildMessage(202, "表单填写已经截止！", null);
            }
        }
        // 验证填写人信息
        String username = (String) questionMap.get("username");
        String password = (String) questionMap.get("password");
        username = username.toUpperCase();
        Student student = studentMapper.getStudentByuserId(username);
        if (student == null){
            return ServiceMessageBuilder
                    .buildMessage(203,"用户不存在！",null);
        }else {
            if (!password.equals(student.getStu_name())){
                return ServiceMessageBuilder
                        .buildMessage(204,"用户名或密码错误!",null);
            }
        }
        int studentId = student.getId();
        QuestionForm oldQuestionForm = questionFormMapper.getFormByQuStuId(questionid,studentId);
        if (oldQuestionForm != null){
            return ServiceMessageBuilder
                    .buildMessage(205,"该用户已经填写过该表单!",null);
        }
        QuestionForm questionForm = new QuestionForm();
        questionForm.setQuestionid(questionid);
        questionForm.setStudentid(studentId);
        ArrayList formContent = (ArrayList)questionMap.get("content");
        String content = QuestionFormUtils.buildAnswerContent(formContent);
        questionForm.setContent(content);
        Integer row = questionFormMapper.submitQuestionForm(questionForm);
        if (row == 1)
            return ServiceMessageBuilder
                    .buildMessage(200,"提交成功!",null);
        else {
            return ServiceMessageBuilder
                    .buildMessage(500,"server error",null);
        }
    }

    public HashMap<String,Object> getPerson(String person_name){
        List<Map> people = studentMapper.getDetailsByStudentPartName("%"+person_name+"%");
        if (people == null || people.size() == 0){
            return ServiceMessageBuilder
                    .buildMessage(404,"没有找到任何人！",null);
        } else {
            return ServiceMessageBuilder
                    .buildMessage(200,"success",people);
        }
    }
}
